/*
 * @Author: 哈尔滨大拐 3181845089@qq.com
 * @Date: 2022-07-27 11:12:05
 * @LastEditors: 哈尔滨大拐 3181845089@qq.com
 * @LastEditTime: 2022-08-08 09:14:06
 * @FilePath: \mygit\个人网站\js\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
(function () {
    const oContainer = document.querySelector('.carousel')
    const oImgBox = oContainer.querySelector('.imgs')
    const oPrev = oContainer.querySelector('.prev')
    const oNext = oContainer.querySelector('.next')
    const aImgs = oContainer.querySelectorAll('.imgs-wrap')
    const moveTimes = aImgs.length - 3 // 能翻页的次数
    let currTime = 0


    oPrev.onclick = oNext.onclick = function () {
        if (this == oPrev) {
            if (currTime > 0) {
                currTime--
                changeTime()
            }
            // console.log('左');
        } else {
            if (currTime < moveTimes) {
                currTime++
                changeTime()
            }
            // console.log('右');
        }
    }

    function changeTime() {
        oImgBox.style.left = -((aImgs[0].offsetWidth + 30) * currTime) + 'px'
        changeBtn()
    }


    function changeBtn() {
        if (currTime <= 0) {
            oPrev.classList.add('invalid')
        } else {
            oPrev.classList.remove('invalid')
        }

        if (currTime >= moveTimes) {
            oNext.classList.add('invalid')
        } else {
            oNext.classList.remove('invalid')
        }
    }

    let timer

    function go() {
        timer = setInterval(() => {
            if (currTime == moveTimes) {
                currTime = -1
            }
            oNext.onclick()
        }, 2000)
    }
    go()

    oContainer.onmouseover = function () {
        clearInterval(timer)
    }
    oContainer.onmouseout = function () {
        go()
    }
})()


